$().ready(function() {

    //移除密码(cookie)
    $('#remember-password').click(function () {
        if (!$('#remember-password').attr('checked')) {
            var cookie = new rememberPassword();
            cookie.cookieDelete();
        }
    });
    //cookie记住密码
    var cookie = new rememberPassword();
    cookie.cookieInit();

    $("#login-main").validate({
        rules:{
            username:{
                required:true,
                checkName:true,
            },
            password:{
                required:true,
                checkPwd:true,
            },
        },
        messages: {
            username: {
                required: "*必填！",
                rangelength: "*长度为2到10位！",
            },
            password: {
                required: "*必填！",
                rangelength: "*长度为6到16位！",
            }
        },
        //提交表单后，（第一个）未通过验证的表单获得焦点
        focusInvalid:true,
        //当未通过验证的元素获得焦点时，移除错误提示
        focusCleanup:true,
    });


    $.validator.addMethod("checkName",function(value,element,params){
        var checkName = /^\w{2,10}$/g;
        return this.optional(element)||(checkName.test(value));
    },"*只允许2-10位英文字母、数字或者下画线！");

    $.validator.addMethod("checkPwd",function(value,element,params){
        var checkPwd = /^\w{6,16}$/g;
        return this.optional(element)||(checkPwd.test(value));
    },"*只允许6-16位英文字母、数字或者下画线！");

    $("#loginsubmit").click(function () {
        var username = $("#username").val();
        var password = $("#password").val();
        $.ajax({
            url : "../userManage/login",
            type : "get",
            contentType : "application/json",
            dataType : "json",
            data : {"username":username,"password":password},
            success : function(data) {
                if(data===1 || data==4 || data==5) //管理员界面
                {
                    if ($('#remember-password').attr('checked')) {
                        var cookie = new rememberPassword();
                        cookie.cookieRemeber();
                    }
                    window.location.href = "frame.html?menu=sys";
                }
                else if (data == 2)
                {
                    if ($('#remember-password').attr('checked')) {
                        var cookie = new rememberPassword();
                        cookie.cookieRemeber();
                    }
                    window.location.href = "home.html";
                }
                else if (data===3)  //单站用户
                {
                    if ($('#remember-password').attr('checked')) {
                        var cookie = new rememberPassword();
                        cookie.cookieRemeber();
                    }
                    window.location.href = "frame.html";
                }else {
                    layer.msg("用户名或密码错误，请重新输入", {icon: 0});
                }
            },
            error : function(){
                layer.msg("用户名或密码错误，请重新输入", {icon: 0});
            }
        })
    });
});

function rememberPassword() {
    //存储变量
    this.username = $('#username').val();
    this.password = $('#password').val();
    this.cookie;
    if (!!$.cookie('user')) {
        this.cookie = eval($.cookie('user'));
    } else {
        $.cookie('user', '[]');
        this.cookie = eval($.cookie('user'));
    };
}
rememberPassword.prototype = {
    cookieInit: function() { //初始化
        var temp = this.cookie,
            username = this.username,
            start = false;
        console.log(temp);
        if (temp.length > 0) {
            $.each(temp, function(i, item) {
                if (item.first == true) {
                    $('#username').val(item.username);
                    $('#password').val(item.password);
                    $('#remember-password').attr('checked', true)
                }
            });
        }
        $('#username').blur(function() {
            console.log('失去焦点');
            //检查是否存在该用户名,存在则赋值，不存在则不做任何操作
            $.each(temp, function(i, item) {
                if (item.username == $('#username').val()) {
                    $('#username').val(item.username);
                    $('#password').val(item.password);
                    $('#remember-password').attr('checked', true)
                    start = true;
                    return false;
                } else {
                    $('#password').val('');
                }

            });
        });
    },
    //记住密码
    cookieRemeber: function() {
        var temp = this.cookie,
            username = this.username,
            password = this.password,
            start = false;
        //检测用户是否存在
        $.each(temp, function(i, item) {
            if (item.username == username) {
                //记录最后一次是谁登录的
                item.first = true;
                $('#password').val(item.password);
                start = true;
                return;
            } else {
                item.first = false;
            }
        });
        //不存在就把用户名及密码保存到cookie中
        if (!start) {
            temp.push({
                username: username,
                password: password,
                first: true
            });
        }
        //存储到cookie中
        $.cookie('user', JSON.stringify(temp));
    },
    //删除密码
    cookieDelete: function() {
        var temp = this.cookie,
            username = this.username,
            num = 0;
        //检测用户是否存在
        $.each(temp, function(i, item) {
            if (item.username === username) {
                num = i;
            }
        });
        //删除里面的密码
        temp.splice(num, 1);
        //存储到cookie中
        $.cookie('user', JSON.stringify(temp));
    }
}